package o12getlastwordlength;

/**
 * @author shkstart
 * @create 2022-01-08-20:46
 */
public class GetLastWordLength {

    public static void main(String[] args) {
        int i = lengthOfLastWord("Hello World");
        System.out.println(i);
    }

    public static int lengthOfLastWord(String s) {

        int res = 0;
        int last = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                if (res !=0) {
                    last = res;
                }
                res = 0;
            }
            else {
                res++;
            }
        }

        return res == 0? last: res;

    }

}
